import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
filename = './lena.jpg'

#rgb分量提取cv.split()
src = cv.imread(filename)

#r,g,b = cv.split(src,mv=None)这里出现错误，通道应该是b、g、r
b,g,r = cv.split(src,mv=None)

cv.imshow('red',r)
cv.imshow('green',g)
cv.imshow('blue',b)

'''
OpenCV读取的图片从BGR转换为RGB
import cv2

img=cv2.imread(‘imagename’)

方法一

b,g,r=cv2.split(img)

img2=cv2.merge([r.g.b])

方法二

img2=img[: , : , : : -1]
'''
#cv.waitKey()
#rgb分量合并cv.merge()
#merge_b = cv.merge([b, np.zeros(b.shape, np.uint8), np.zeros(b.shape, np.uint8)])
#merge = cv.merge([r,g,b])
#cv.imshow('merge',merge)
#cv.waitKey()


#hsv分量显示
hsv = cv.cvtColor(src,cv.COLOR_BGR2HSV)
cv.imshow('hsv',hsv)
hsvs=cv.split(hsv)
cv.imshow('Hue',hsvs[0])
cv.imshow('Saturation',hsvs[1])
cv.imshow('Value',hsvs[2])
cv.waitKey()





#c++
'''
#include <cv.h>
#include <highgui.h>
 
int main(int argc, char* argv[])
{
	IplImage* pSrcImage = cvLoadImage("D:\\cvImg\\lin.jpg");
	IplImage* pR_Plane = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);
	IplImage* pG_Plane = cvCloneImage(pR_Plane);
	IplImage* pB_Plane = cvCloneImage(pR_Plane);
 
	cvSplit(pSrcImage, pB_Plane, pG_Plane, pR_Plane, NULL);	//这个函数执行后的结果是单通道图片，显示出来是灰度图。NND，需要重新Merge下。
 
	IplImage* pR_Channel = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 3);
	cvZero(pR_Channel);	//如果没有这一句，你可以想象结果是什么样子，创建的图像中数据没有初始化，Merge的时候会乱的。
	IplImage* pG_Channel = cvCloneImage(pR_Channel);
	IplImage* pB_Channel = cvCloneImage(pR_Channel);
 
	cvMerge(NULL, NULL, pR_Plane, NULL, pR_Channel);
	cvMerge(NULL, pG_Plane, NULL, NULL, pG_Channel);
	cvMerge(pB_Plane, NULL, NULL, NULL, pB_Channel);
 
	cvShowImage("Source", pSrcImage);
	cvShowImage("R Channel", pR_Channel);
	cvShowImage("G Channel", pG_Channel);
	cvShowImage("B Channel", pB_Channel);
	cvWaitKey();
	return 0;
}

'''